package com.ff.xyh.common.entity.result;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther FF
 * @Date 2022年 09月 18日
 * @Time 12:49
 */

@Data
public class ResultObject {

    @ApiModelProperty(value = "是否成功")
    private Boolean isSuccess;

    @ApiModelProperty(value = "结果状态码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "响应数据")
    private Map<String, Object> data;

    private ResultObject(){

    }

    public static ResultObject ok(){
        ResultObject resultObject = new ResultObject();
        resultObject.setIsSuccess(true);
        resultObject.setCode(ResultCode.SUCCESS_CODE);
        resultObject.setMessage("成功");
        return resultObject;
    }

    public static ResultObject error(){
        ResultObject resultObject = new ResultObject();
        resultObject.setIsSuccess(false);
        resultObject.setCode(ResultCode.ERROR_CODE);
        resultObject.setMessage("失败");
        return resultObject;
    }

    /**
     * 可暴露错误信息的错误
     * @return
     */
    public static ResultObject exposed_error(){
        ResultObject resultObject = new ResultObject();
        resultObject.setIsSuccess(false);
        resultObject.setCode(ResultCode.Exposed_ERROR_CODE);
        return resultObject;
    }
    /**
     * 需要处理错误信息的错误
     * @return
     */
    public static ResultObject pending_error(String message){
        ResultObject resultObject = new ResultObject();
        resultObject.setIsSuccess(false);
        resultObject.setCode(ResultCode.Pending_ERROR_CODE);
        resultObject.setMessage(message);
        return resultObject;
    }



    public ResultObject isSuccess(boolean isSuccess){
        this.setIsSuccess(isSuccess);
        this.setCode(isSuccess ? ResultCode.SUCCESS_CODE : ResultCode.ERROR_CODE);

        return this;
    }
//    public ResultObject resultCode(Integer resultCode){
//        this.setResultCode(resultCode);
//        return this;
//    }
    public ResultObject message(String message){
        this.setMessage(message);
        return this;
    }
    public ResultObject data(Map<String, Object> data){
        this.setData(data);
        return this;
    }

    public ResultObject data(String name,Object data){
        if (this.data == null)
            this.data = new HashMap<>();
        this.data.put(name, data);
        return this;
    }

    public ResultObject putData(Object data){
        if (this.data == null)
            this.data = new HashMap<>();
        this.data.put("data", data);
        return this;
    }
    public ResultObject putList(List list, long total){
        if (this.data == null)
            this.data = new HashMap<>();
        this.data.put("items", list);
        this.data.put("total", total);
        return this;
    }
    public ResultObject changeList(List list){
        if (this.data == null)
            this.data = new HashMap<>();
        this.data.put("items", list);
        return this;
    }


    /**
     * 存放数据时，若数据为空则设置为失败
     * @param name
     * @param data
     * @return
     */
    public ResultObject setDataAndCheckNull(String name,Object data){
        if (this.data == null)
            this.data = new HashMap<>();
        if (data == null)
            return this.isSuccess(false).message("结果为空");
        else
            this.data.put(name, data);
        return this;
    }

}
